package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;
    /*登录业务的方法*/
    @Override
    public R<Employee> login(Employee employee) {
        //1.验证是否存在 条用mapper 根据用户查找对象
        Employee loginEmployee = employeeMapper.login(employee);
        //判断是否为null,如果为空用户不存在,提示用户
        if (loginEmployee == null) {
            throw new BusinessException("用户名不存在,请注册在来");
        }
            //2.校验密码是否正确
            //.对用户名加密处理
            String md5 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

            if (!loginEmployee.getPassword().equals(md5)) {
                //如果不一样,提示用户修改
                throw new BusinessException("用户名或密码错误");
            }

            //3.校验是否禁用
        if (loginEmployee.getStatus() == 0) {
            throw new BusinessException("您的账号已禁用,请充值");
        }
        return R.success(loginEmployee);
    }
    @Override
    public boolean save(Employee employee) {
        //1校验账号是否唯一
        //根据账号查询用户
        Employee dbEmployee = employeeMapper.login(employee);
        //判断账号是否为Null,如果不为NUll 就说明存在,提示用户重新添加
        if (dbEmployee!=null) {
            throw  new BusinessException("账号已备注册");
        }

        //2.完善数据
        //默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));


        //状态  1 启用
        employee.setStatus(1);

        //创建时间
        employee.setCreateTime(LocalDateTime.now());

        //修改时间
        employee.setUpdateTime(LocalDateTime.now());

        //调用mapper添加到数据库
        employeeMapper.save(employee);

        return true;
    }

    @Override
    public Page<Employee> findByPage(Integer page, Integer pageSize, String name) {
        //1.设置分页查询第几页多少条
        PageHelper.startPage(page,pageSize);
        //2.执行查询所有数据的sql语句
        List<Employee> employeeList = employeeMapper.findByName(name);
        //3.使用pageInfo 封装上面的结果
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);

        Page<Employee> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());

        return pageObj;

    }

    @Override
    public boolean update(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        return employeeMapper.update(employee)>0;

    }

    @Override
    public R<Employee> findById(Long id) {
        Employee employee = employeeMapper.findById(id);

        return R.success(employee);
    }


}
